package com.mla.fastdfs.controller;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.mla.fastdfs.commom.Result;
import com.mla.fastdfs.dto.RolePermissionDto;
import com.mla.fastdfs.dto.RoleSaveDto;
import com.mla.fastdfs.dto.UserRoleSaveDto;
import com.mla.fastdfs.pojo.Role;
import com.mla.fastdfs.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * z
 *
 * @author Jun
 * @version v1.0
 * @date 2020-08-17
 */
@RestController
@RequestMapping(value = "/role")
public class RoleController {
    @Autowired
    private RoleService roleservice;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(RoleSaveDto record) {
        if(StringUtil.isEmpty(record.getCode())){
            return Result.error("角色编号不能为空");
        }
        if(StringUtil.isEmpty(record.getRoleName())){
            return Result.error("角色编码不能为空");
        }
        return roleservice.saveRole(record);
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Result del(Integer id) {
        if (id == null) {
            return Result.error("id不能为空");
        }

        roleservice.del(id);
        return Result.ok();
    }

    @RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.POST)
    public Result selectByPrimaryKey(Integer id) {
        if (id == null) {
            return Result.error("id不能为空");
        }

        Role info = roleservice.selectByPrimaryKey(id);
        return Result.ok(info);
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result getList(Role record) {
        /**条件判断**/


        List<Role> list = roleservice.getList(record);
        return Result.ok(list);
    }

    @RequestMapping(value = "/pageList", method = RequestMethod.POST)
    public Result pageList(Role record,
                                 @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                 @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) {
        /**条件判断**/


        PageInfo<Role> info = roleservice.pageList(record, pageNo, pageSize);
        return Result.ok(info);
    }

    @RequestMapping(value = "/userRoleSave", method = RequestMethod.POST)
    public Result userRoleSave(UserRoleSaveDto record) {
        if(record.getUserId() == null){
            return Result.error("用户不能为空");
        }
        if(record.getRoleId() == null){
            return Result.error("角色不能为空");
        }
        return roleservice.userRole(record);
    }

    @RequestMapping(value = "/roleInfo", method = RequestMethod.POST)
    public Result roleInfo(RolePermissionDto record) {
        if(record.getRoleId() == null){
            return Result.error("角色参数不能为空");
        }
        return roleservice.roleInfo(record);
    }

}